package com.qf.controller;

import com.qf.convert.DateConvert;
import com.qf.pojo.PageBean;
import com.qf.pojo.Product;
import com.qf.service.ProductService;
import com.qf.service.impl.ProductServiceImpl;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品的后端访问地址
 */
@WebServlet( "/product")
public class ProductServlet extends BaseServlet {

    /**
     * 通过商品id获取商品实体
     * @param request
     * @param response
     */
    public void getProductByPid(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //${pageContext.request.contextPath}/product?methodName=getProductByPid&pid=${p.pid}
        //获取pid
        String pid = request.getParameter("pid") ;
        System.out.println(pid) ;

        //调用商品的业务接口
        ProductService productService = new ProductServiceImpl() ;
        Product product = productService.getProduct(pid) ;
        System.out.println(product) ;
        if(product!=null){
            //存储在request域中
            request.setAttribute("product",product);
        }
        //请求转发到商品详情页
        request.getRequestDispatcher("/jsp/product_info.jsp").forward(request,response);
    }


    /**
     * 商品的分页查询
     * @param request 请求对象
     * @param response 响应对象
     */
    public void getProductByPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        //http://localhost:8080/Servlet_Jdbc_Jsp_war_exploded/product?methodName=getProductByPage&cid=1

        //获取地址栏上的参数
        String cid = request.getParameter("cid");
        //System.out.println("商品的分类id是:"+cid);

        //获取前端传过来的当前页码/每页显示的条数
        String curPage = request.getParameter("curPage");
        String pSize = request.getParameter("pSize");
        if(curPage==null || "".equals(curPage)){
            //前端没有传过来
            curPage = "1" ; //当前页码
        }
        if(pSize == null || "".equals(pSize)){
            pSize = "12" ; //每页显示的条数
        }

        //将curPage /pSize转换成int
        int currentPage = Integer.parseInt(curPage);
        int pageSize = Integer.parseInt(pSize);

        //调用商品的业务接口
        ProductService productService = new ProductServiceImpl() ;
        PageBean<Product> pb =  productService.getProductByPage(cid,currentPage,pageSize) ;
        if(pb!=null){
            //存储在request 域中
            request.setAttribute("pb",pb);
        }
        //请求转发到/product_list.jsp
        request.getRequestDispatcher("/jsp/product_list.jsp").forward(request,response);

    }

    //管理员查询查询所有商品
    public void findAllProduct(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String curPage = request.getParameter("curPage");
        String pSize = request.getParameter("pSize");
        if(curPage==null||"".equals(curPage)){
            curPage = "1" ;
        }
        if(pSize==null ||"".equals(pSize)){
            pSize = "15" ;
        }
        int currentPage = Integer.parseInt(curPage);
        int pageSize = Integer.parseInt(pSize);

        ProductService productService = new ProductServiceImpl() ;
        PageBean<Product> pb = productService.getAllProductByPage(currentPage,pageSize) ;
        request.setAttribute("pb",pb);
        request.getRequestDispatcher("/admin/product/product_list.jsp").forward(request,response);
    }
}
